package com.yjzx.util.binlog.core.util;

import com.yjzx.util.binlog.core.config.BinLogBean;
import com.yjzx.util.binlog.core.entiry.recordIndex.BinaryLogClientIndex;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author yjzx
 * @date 2024/8/15
 * @description
 */
@Slf4j
public class BinLogUtils {

    /**
     * 拼接dbTable
     */
    public static String getdbTable(String db, String table) {

        return nameToStr(db) + "-" + nameToStr(table);
    }
    public static String nameToStr(String name) {
        if (name == null) {
            return null;
        }else {
            if (name.startsWith("`")&&name.endsWith("`")&&name.length()>2) {
                return name.substring(1, name.length()-1);
            }
            return name;

        }
    }


    /**
     * 根据DBTable获取table
     *
     * @param dbTable
     * @return java.lang.String
     */
    public static String getTable(String dbTable) {
        if (StringUtils.isEmpty(dbTable)) {
            return "";
        }
        String[] split = dbTable.split("-");
        if (split.length == 2) {
            return split[1];
        }
        return "";
    }

    /**
     * 将逗号拼接字符串转List
     *
     * @param str
     * @return
     */
    public static List<String> getListByStr(String str) {
        if (StringUtils.isEmpty(str)) {
            return Collections.emptyList();
        }

        return Arrays.asList(str.split(","));
    }

    public static void toFileRecordBinLogIndex(BinLogBean binLogBean, List<BinaryLogClientIndex> indexs) {
        BinaryLogClientIndex[] indices = new BinaryLogClientIndex[0];
        toFileRecordBinLogIndex(binLogBean, indexs==null?indices:indexs.toArray(indices));
    }
    public static void toFileRecordBinLogIndex(BinLogBean binLogBean, BinaryLogClientIndex... index) {
        Map<Integer, List<BinaryLogClientIndex>> collect = Stream.of(index).collect(Collectors.groupingBy(BinaryLogClientIndex::getOrder, Collectors.toList()));
        collect.keySet().stream().sorted()
                .forEach(order -> collect.get(order).forEach(binLogIndex -> {
                            if (binLogIndex.valid()) {
                                if (binLogIndex.isRead()){
                                    binLogBean.setReadBinLogIndex( binLogIndex);
                                }
                                if (binLogIndex.isWrite()) {
                                    Set<BinaryLogClientIndex> set;
                                    if ((set=binLogBean.getWriteBinLogIndex())==null) {
                                        set = new HashSet<>();
                                    }
                                    set.add(binLogIndex);
                                }
                            }
                        })
                );
        if (binLogBean.getReadBinLogIndex()==null){
            binLogBean.setReadBinLogIndex(BinaryLogClientIndex.empty());
        }
        if (binLogBean.getWriteBinLogIndex()==null){
            binLogBean.setWriteBinLogIndex(Collections.singleton(BinaryLogClientIndex.empty()));
        }
    }
}
